const Koa = require('koa');
const path = require('path');
const app = new Koa();
const fs = require('fs');
app.use(async ctx => {
    const { url } = ctx;

    if (url === '/') {
        console.log('url', url);
        console.log('ctx', ctx);
        ctx.type = 'text/html'
        let content = fs.readFileSync('./index.html', 'utf-8')
        ctx.body = content;
    } else if (url.endsWith('.js')) {
        console.log('url', url);
        // js文件
        const p = path.resolve(__dirname, url.slice(1))
        ctx.type = 'application/javascript';
        const content = fs.readFileSync(p, 'utf-8')
        ctx.body = content
        console.log('p', p);
    }
});

let listens = 3300;

app.listen(listens, () => {
    console.log(`监听端口${listens}`);
})
